    #include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    :QWidget (parent)
{
    this->playcode = new PlaycodeVideo();
    this->playcode->initPlayCode();
    this->playcode->initRGB();
    this->playcode->initYUV();
    this->init_window();
    this->init_control();
    this->init_connect();
    this->init_qss();
}

MainWidget::~MainWidget()
{

}

void MainWidget::init_window()
{
    this->setWindowTitle("登陆界面");
    this->resize(700,700);
}

void MainWidget::init_control()
{
    this->loginwin = new LoginWidget;

    this->title = new QLabel("执法记录仪主界面", this);
    this->title->move(200,50);
    this->title->setStyleSheet("font-size:40px;font-weight:bold");

    //实时录像
//    this->playcode->start();
//    this->decode->start();
    //按钮
    this->takePhotoBtn = new QPushButton("现场拍照", this);
    this->takePhotoBtn->move(150,530);
    this->replayBtn = new QPushButton("录像回放", this);
    this->replayBtn->move(300,530);
    this->viewPhotoBtn = new QPushButton("现场图片", this);
    this->viewPhotoBtn->move(450,530);
    this->viewLogBtn = new QPushButton("查看日志", this);
    this->viewLogBtn->move(300,580);
    this->trancevideoBtn = new QPushButton("录像转码", this);
    this->trancevideoBtn->move(150,630);
    this->setBtn = new QPushButton("系统设置", this);
    this->setBtn->move(300,630);
    this->LoginBackBtn = new QPushButton("登录", this);
    this->LoginBackBtn->move(450,630);


}

void MainWidget::init_connect()
{
//    connect(this->decode, SIGNAL(sendImg(QImage)), this, SLOT(receiveImg(QImage)));
    connect(this->playcode, SIGNAL(sendImg(QImage)), this, SLOT(receiveImg(QImage)));
    connect(this->LoginBackBtn, SIGNAL(clicked()), this, SLOT(toLoginWin()));
    //去登陆界面
    connect(this->loginwin, SIGNAL(backToMain1()), this, SLOT(showMain1()));
    connect(this->loginwin, SIGNAL(backToMain2()), this, SLOT(showMain2()));
    //返回设置界面
    connect(this->setBtn, SIGNAL(clicked()), this, SIGNAL(backToSet()));

    //拍照
    connect(this->takePhotoBtn, SIGNAL(clicked()), this, SLOT(takePhoto()));
    //去拍照界面
    connect(this->viewPhotoBtn, SIGNAL(clicked()), this, SLOT(toPhotoWin()));

    //录像回放界面
    connect(this->replayBtn, SIGNAL(clicked()), this, SLOT(toRePlayWin()));


    //查看日志界面
    connect(this->viewLogBtn, SIGNAL(clicked()), this, SLOT(toViewLogWin()));
    //录像转码界面
    connect(this->trancevideoBtn, SIGNAL(clicked()), this, SLOT(toTranceWin()));

}

void MainWidget::init_qss()
{
    QFile file(":/img/Main.qss");
    if(file.open(QFile::ReadOnly))
    {
        this->setStyleSheet(file.readAll());
        file.close();
    }
}

PlaycodeVideo *MainWidget::getPlaycode() const
{
    return playcode;
}

void MainWidget::receiveImg(QImage img)
{
    this->image = img;

    //手动触发paintEvent重绘事件
    update();
}

void MainWidget::showMain1()
{

    this->show();
    this->loginwin->hide();
}

void MainWidget::showMain2()
{
    qDebug() << "sdasdsada";
    this->showMain1();
    this->LoginBackBtn->setText("退出");

    //登录成功后开始录屏
    this->playcode->encode->setIsRun(true);
}

void MainWidget::toLoginWin()
{
    if(this->LoginBackBtn->text()=="登录")
    {
        this->hide();
        this->loginwin->show();
    }
    else {
        this->LoginBackBtn->setText("登录");
        UserModel::getInstance()->updateUserAll();
        this->playcode->encode->setIsRun(false);
    }


}

void MainWidget::toPhotoWin()
{

    if(this->LoginBackBtn->text()=="登录"){
        QMessageBox::warning(this, "主界面", "请先登录");
        this->loginwin->show();
        this->hide();
        return ;
    }
    this->photowin = new PhotoWidget;

    //拍照界面返回
    connect(this->photowin, SIGNAL(backToMain()), this, SLOT(showMain3()));

    this->photowin->show();
    this->hide();
}

void MainWidget::showMain3()
{
    this->show();
    this->photowin->close();
}

void MainWidget::takePhoto()
{
    if(this->LoginBackBtn->text()=="登录"){
        QMessageBox::warning(this, "主界面", "请先登录");
        this->loginwin->show();
        this->hide();
        return ;
    }

    //设置视频保存的路径
    char **qres = nullptr;
    int col=0,row=0;
    int res = ConfigureModel::getInstance()->selectLastImagePath(qres, row, col);
    QString imagepath = qres[1];

    //获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    //获取当前时间
    QTime currentTime = currentDateTime.time();
    //获取当前日期
    QDate currentDate = currentDateTime.date();
    QString date = currentDateTime.toString("yyyy_MM_dd_hh_mm_ss");
    QString datetime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
//    QString filename = "../photo/" + date + ".jpg";
    QString filename = date + ".jpg";

    this->image.save(QString("%2/%1").arg(filename).arg(imagepath));
    //计算该图片的大小

    imagepath = QString("%1/"+date+".jpg").arg(imagepath);
    QFileInfo fileInfo(imagepath);
    qint64  size;
    if (fileInfo.exists() && fileInfo.isFile()) {
        size = fileInfo.size();
        qDebug() << "File size in bytes:" << size;
    } else {
        qDebug() << imagepath;
        qDebug() << "File does not exist or is not a valid file.";
    }

    double ksize = size/1024.0;
    QString result = QString::number(ksize, 'f', 2);
    ksize = result.toDouble();

    qDebug() << size << "  " << ksize;
    qDebug() << imagepath;



    //获取登录的用户id
    qres=nullptr;
    col = 0,row=0;
    res = UserModel::getInstance()->selectUserId(qres, row, col);
    res = ImageModel::getInstance()->InsertImage(imagepath, datetime,ksize, atoi(qres[1]));

    QString name;
    if(atoi(qres[1])==1)
    {
        name = "100101";
    }
    else if(atoi(qres[1])==2) {
        name = "100102";
    }
    else {
        name = "100103";
    }

    qres=nullptr;
    col = 0,row=0;
    ConfigureModel::getInstance()->selectLastConfigure(qres, row, col);
    res = LogModel::getInstance()->InsertLog(datetime, qres[1], name, "拍照");

    if(res==0){
        qDebug() << "take a photo : " << filename;
    }

    //img.save(QString("../fileout/%1.jpg").arg(x));
}

void MainWidget::toRePlayWin()
{
    if(this->LoginBackBtn->text()=="登录"){
        QMessageBox::warning(this, "主界面", "请先登录");
        this->loginwin->show();
        this->hide();
        return ;
    }
    this->playWidget = new PlayBackWidget;
    this->playWidget->show();
    connect(this->playWidget, SIGNAL(backToMain()), this, SLOT(showMain4()));
    this->hide();
}

void MainWidget::showMain4()
{
    this->playWidget->close();
    this->show();
}

void MainWidget::toViewLogWin()
{
    if(this->LoginBackBtn->text()=="登录"){
        QMessageBox::warning(this, "主界面", "请先登录");
        this->loginwin->show();
        this->hide();
        return ;
    }
    this->hide();
    this->logWidget = new LogWidget;
    this->logWidget->show();
    connect(this->logWidget, SIGNAL(backToMain()), this, SLOT(showMain6()));

}

void MainWidget::toTranceWin()
{
    if(this->LoginBackBtn->text()=="登录"){
        QMessageBox::warning(this, "主界面", "请先登录");
        this->loginwin->show();
        this->hide();
        return ;
    }

    this->trancewidget = new TranceWidget;
    this->trancewidget->show();
    connect(this->trancewidget, SIGNAL(backToMain()), this, SLOT(showMain5()));
    this->hide();
}

void MainWidget::showMain5()
{
    this->trancewidget->close();
    this->show();
}

void MainWidget::showMain6()
{
    this->logWidget->close();
    this->show();
}

void MainWidget::closeEvent(QCloseEvent *event)
{

    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
                tr("Are you sure you want to exit?\n"),
                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                QMessageBox::Yes);

    if (resBtn != QMessageBox::Yes) {

        event->ignore();
    } else {
        this->playcode->encode->writeTalier();
        UserModel::getInstance()->updateUserAll();
        event->accept();
    }

}

void MainWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    //画笔对象
    QPainter paint(this);
    //判断Image是不是空的
    if(!this->image.isNull())
    {
        paint.drawImage(QRect(25,100,650,400), this->image);
    }
}
